package com.elitel.frame.base.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.component.RedisCacheComponent;
import com.elitel.frame.base.service.BaseMasterdetailService;
import com.elitel.frame.base.service.BaseShowfieldService;
import com.elitel.frame.main.dao.BaseMasterdetailMapper;
import com.elitel.frame.main.dao.CfgExtmasterMapper;
import com.elitel.frame.main.dao.ext.ExtBaseMasterdetailMapper;
import com.elitel.frame.main.dao.ext.ExtBaseShowfieldMapper;
import com.elitel.frame.main.entity.BaseMasterdetail;
import com.elitel.frame.main.entity.BaseShowfield;
import com.elitel.frame.main.entity.CfgExtmaster;
import com.elitel.frame.main.entity.ext.BaseMasterdetailExt;
import com.elitel.frame.main.entity.vo.ShowFieldVo;
import com.elitel.frame.base.component.RedisCacheCfgMaster;

/**
 * @Description 主从表配置中从表服务接口实现
 * @author guoyanfei
 */
@Service
public class BaseMasterdetailServiceImpl implements BaseMasterdetailService {
	@Autowired
	private BaseMasterdetailMapper baseMasterdetailMapper;
	@Autowired
	private ExtBaseMasterdetailMapper extBaseMasterdetailMapper;
	@Autowired
	private ExtBaseShowfieldMapper extBaseShowfieldMapper;
	@Autowired
	private BaseShowfieldService baseShowfieldService;
	@Autowired
	private CfgExtmasterMapper cfgExtmasterMapper;
	@Autowired
	private RedisCacheCfgMaster redisCacheCfgMaster;
	
	@Override
	public Integer insertMasterDetail(BaseMasterdetail baseMasterdetail) {
		Integer result = 0;
		baseMasterdetail.setGuid(UUID.randomUUID().toString());
		Date nowdate = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		baseMasterdetail.setCreatedate(dateFormat.format(nowdate));
		result = baseMasterdetailMapper.insertSelective(baseMasterdetail);
		if(result > 0){
			//刷新从表缓存
			redisCacheCfgMaster.refreshCfgMasterdetail(baseMasterdetail.getMguid());
		}
		return result;
	}

	@Override
	public Integer updateMasterDetail(BaseMasterdetail baseMasterdetail) {
		Integer result = 0;
		//修改时，检查是否修改从表名称，如修改，则修改删除已配置的显示字段
		BaseMasterdetail baseMasterdetailold = baseMasterdetailMapper.selectByPrimaryKey(baseMasterdetail.getGuid());
		if(!baseMasterdetail.getDtConfigId().equals(baseMasterdetailold.getDtConfigId())){
			//删除显示字段
			extBaseShowfieldMapper.deleteShowfieldByServiceIdandMasterDetailID(baseMasterdetail.getMguid() ,baseMasterdetail.getGuid());
		}
		result = baseMasterdetailMapper.updateByPrimaryKeySelective(baseMasterdetail);
		if(result > 0){
			//刷新从表缓存
			redisCacheCfgMaster.refreshCfgMasterdetail(baseMasterdetail.getMguid());
		}
		return result;
	}

	@Override
	public Integer deleteMasterDetail(String guid) {
		Integer result = 0;
		BaseMasterdetail baseMasterdetail = baseMasterdetailMapper.selectByPrimaryKey(guid);
		if(baseMasterdetail != null){
			List<BaseShowfield> baseShowfields = extBaseShowfieldMapper.getShowfieldByServiceId(baseMasterdetail.getMguid(),2,guid);
			if(baseShowfields != null && baseShowfields.size() > 0){
				String baseShowguid = "";
				for (BaseShowfield baseShowfield : baseShowfields) {
					if(baseShowguid != null && !baseShowguid.equals("")){
						baseShowguid+=","+baseShowfield.getGuid();
					}else{
						baseShowguid+=baseShowfield.getGuid();
					}
				}
				//删除从表显示字段
				result += baseShowfieldService.deleteShowfieldByguids(baseShowguid);
			}
		}
		//删除从表
		result += baseMasterdetailMapper.deleteByPrimaryKey(guid);
		//刷新从表缓存
		redisCacheCfgMaster.refreshCfgMasterdetail(baseMasterdetail.getMguid());
		return result;
	}

	@Override
	public List<BaseMasterdetail> getMasterdetailByguid(String mguid) {
		return extBaseMasterdetailMapper.getBaseMasterdetailbymguid(mguid);
	}

	@Override
	public List<BaseMasterdetailExt> getExtMasterdetailBymguid(String mguid) {
		List<BaseMasterdetailExt> newbaseMasterdetailExts = new ArrayList<BaseMasterdetailExt>();
		List<BaseMasterdetailExt> baseMasterdetailExts = extBaseMasterdetailMapper.getExtBaseMasterdetailbymguid(mguid);
		if(baseMasterdetailExts != null && baseMasterdetailExts.size() > 0){
			for (BaseMasterdetailExt baseMasterdetailExt : baseMasterdetailExts) {
				//设置从表关联字段其他信息
				List<ShowFieldVo> showFieldVos = baseShowfieldService.getAllShowFieldVo(baseMasterdetailExt.getDtConfigId());
				if(showFieldVos != null && showFieldVos.size() > 0){
					for (ShowFieldVo showFieldVo : showFieldVos) {
						if(showFieldVo.getFieldguid().equals(baseMasterdetailExt.getDfield())){
							baseMasterdetailExt.setAltertablename(showFieldVo.getAltertablename());
							baseMasterdetailExt.setDfieldCh(showFieldVo.getFieldnameCn());
							baseMasterdetailExt.setDfieldname(showFieldVo.getFieldname());
						}
					}
				}
				
				//设置主表关联字段其他信息
				CfgExtmaster cfgExtmaster = cfgExtmasterMapper.selectByPrimaryKey(mguid);
				if(cfgExtmaster != null){
					List<ShowFieldVo> showFieldVos2 = baseShowfieldService.getAllShowFieldVo(cfgExtmaster.getDtConfigId());
					if(showFieldVos2 != null && showFieldVos2.size() > 0){
						for (ShowFieldVo showFieldVo : showFieldVos2) {
							if(showFieldVo.getFieldguid().equals(baseMasterdetailExt.getMfield())){
								baseMasterdetailExt.setMfieldname(showFieldVo.getFieldname());
								baseMasterdetailExt.setMfieldCh(showFieldVo.getFieldnameCn());
							}
						}
					}
				}
				newbaseMasterdetailExts.add(baseMasterdetailExt);
				
			}
		}
		return newbaseMasterdetailExts;
	}
	
	

}
